package com.alks.function.service.impl.finance.lotcost;

import com.alks.entity.data.enums.LaborTypeEnum;
import com.alks.entity.data.enums.MtlEnum;
import com.alks.function.data.request.finance.lotcost.LotCostRequest;
import com.alks.function.data.vo.finance.lotcost.*;
import com.alks.function.data.vo.finance.lotcost.h5.LotCostAnalysisDetailH5VO;
import com.alks.function.data.vo.finance.lotcost.h5.LotCostAnalysisListH5VO;
import com.alks.function.data.vo.finance.lotcost.h5.LotCostAnalysisStatisticH5VO;
import com.alks.function.mapper.finance.lotcost.LotCostAnalysisMapper;
import com.alks.function.service.finance.lotcost.ILotCostAnalysisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author: chen
 * @date: 2024/12/20
 * @description:
 */
@Service
public class LotCostAnalysisServiceImpl implements ILotCostAnalysisService {

    @Resource
    private LotCostAnalysisMapper lotCostAnalysisMapper;

    @Override
    public List<LotCostAnalysisListVO> tempQuery(LotCostRequest request) {
        return lotCostAnalysisMapper.listPage(request);
    }

    @Override
    public LotCostAnalysisDetailVO detail(String lotNo) {
        LotCostAnalysisDetailVO res = lotCostAnalysisMapper.getDetail(lotNo);
        //面材
        List<ReportMtlDetailVO> upperMtlItems = lotCostAnalysisMapper.getCostDetail(lotNo, MtlEnum.TYPE_UPPER.getValue(), null);
        BigDecimal upperMtlTotal = upperMtlItems.stream()
                .map(e -> e.getAmt() == null ? BigDecimal.ZERO : e.getAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setUpperMtlItems(new LotCostAnalysisDetailVO.Items<>(upperMtlItems, upperMtlTotal, null));
        //面材多购
        List<ReportMtlDetailVO> upperMtlMulItems = lotCostAnalysisMapper.getCostDetail(lotNo, MtlEnum.TYPE_UPPER2.getValue(), null);
        BigDecimal upperMtlMulTotal = upperMtlMulItems.stream()
                .map(e -> e.getAmt() == null ? BigDecimal.ZERO : e.getAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setUpperMtlMulItems(new LotCostAnalysisDetailVO.Items<>(upperMtlMulItems, upperMtlMulTotal, null));
        //大底
        List<ReportMtlDetailVO> soleMtlItems = lotCostAnalysisMapper.getCostDetail(lotNo, MtlEnum.TYPE_SOLE.getValue(), MtlEnum.PART_SOLE.getValue());
        BigDecimal soleMtlTotal = soleMtlItems.stream()
                .map(e -> e.getAmt() == null ? BigDecimal.ZERO : e.getAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setSoleMtlItems(new LotCostAnalysisDetailVO.Items<>(soleMtlItems, soleMtlTotal, null));
        //面衬
        List<ReportMtlDetailVO> soleMtl2Items = lotCostAnalysisMapper.getCostDetail(lotNo, MtlEnum.TYPE_SOLE.getValue(), MtlEnum.PART_FACE.getValue());
        BigDecimal soleMtl2Total = soleMtl2Items.stream()
                .map(e -> e.getAmt() == null ? BigDecimal.ZERO : e.getAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setSoleMtl2Items(new LotCostAnalysisDetailVO.Items<>(soleMtl2Items, soleMtl2Total, null));
        //补单费用
        List<SupplementDutyVO> supplementDutyItems = lotCostAnalysisMapper.getSupplementDutyDetail(lotNo);
        BigDecimal supplementDutyTotal = supplementDutyItems.stream()
                .map(e -> e.getTotalAmt() == null ? BigDecimal.ZERO : e.getTotalAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal supplementDutyTotal2 = supplementDutyItems.stream()
                .map(e -> e.getAmt() == null ? BigDecimal.ZERO : e.getAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setSupplementDutyItems(new LotCostAnalysisDetailVO.Items<>(supplementDutyItems, supplementDutyTotal, supplementDutyTotal2));
        //其他费用
        List<OtherFeeVO> otherFeeItems = lotCostAnalysisMapper.getOtherFeeDetail(lotNo);
        BigDecimal otherFeeTotal = otherFeeItems.stream()
                .map(e -> e.getAmt() == null ? BigDecimal.ZERO : e.getAmt()).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setOtherFeeItems(new LotCostAnalysisDetailVO.Items<>(otherFeeItems, otherFeeTotal, null));
        return res;
    }

    @Override
    public List<ReportMtlDetailVO> getCostDetail(String lotNo, String mtlType, String partName) {
        return lotCostAnalysisMapper.getCostDetail(lotNo, mtlType, partName);
    }

    @Override
    public List<ReportLaborDetailVO> getLaborDetail(String lotNo, String type) {
        if (LaborTypeEnum.MANUAL.getValue().equals(type)) {
            return lotCostAnalysisMapper.getLaborDetail2(lotNo);
        } else if (LaborTypeEnum.OUT.getValue().equals(type)) {
            return lotCostAnalysisMapper.getLaborDetail3(lotNo);
        }else {
            return lotCostAnalysisMapper.getLaborDetail(lotNo, type);
        }
    }

    @Override
    public List<LotCostAnalysisListH5VO> tempQueryForH5(LotCostRequest request) {
        return lotCostAnalysisMapper.listPageForH5(request);
    }

    @Override
    public LotCostAnalysisDetailH5VO detailForH5(String lotNo) {
        return lotCostAnalysisMapper.getDetailForH5(lotNo);
    }

    @Override
    public LotCostAnalysisStatisticH5VO statisticForH5(String lotNo) {
        return lotCostAnalysisMapper.statisticForH5(lotNo);
    }

    @Override
    public List<SupplementDutyVO> getSupplementDutyDetail(String lotNo) {
        return lotCostAnalysisMapper.getSupplementDutyDetail(lotNo);
    }

    @Override
    public List<OtherFeeVO> getOtherFeeDetail(String lotNo) {
        return lotCostAnalysisMapper.getOtherFeeDetail(lotNo);
    }
}
